﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
#region using for special implements
using Com.ProjectA.DAO;
using Com.ProjectA.BusinessLogic;
using Com.ProjectA.FAMvc.Areas.admin.Models;
using Com.ProjectA.FAMvc.MvcHtmlHelper;
using Com.ProjectA.Utility;
#endregion using for special implements
namespace Com.ProjectA.FAMvc.Areas.admin.Controllers
{
    public class MemMgnController : Controller
    {
        private string[] arrPerCd = new string[] { "01003004", "01003001", "01001001001002", "01001001001001001003", "01001001001001002002", "01001001001001002001"
                                             , "01001002", "01002001", "01002001001001", "01002001001002", "01001001"
                                             , "01002002001001", "01001001001001", "01001002001001", "01001001001001001002", "01001001001001001001", "01001001001002002001"};

        // GET: /admin/MemMgn/
        public ActionResult Index(Nullable<Int32> id)
        {
            #region Declare local variables
            List<DAOPerson> lsPerson = new List<DAOPerson>();
            DAOPerson daoPerson = null;
            BLPersonManager perMgnBl = null;
            string msgNotify = null;
            MvcHtmlString mvcHtmlString = null;
            #endregion Declare local variables
            try
            {
                perMgnBl = new BLPersonManager(Constants.STR_CONN_NAME);
                msgNotify = perMgnBl.ListPerson(out lsPerson);
                #region For Testing
                //foreach (string strPerCd in arrPerCd)
                //{
                //    DAOPerson person = new DAOPerson()
                //    {
                //        person_code = strPerCd
                //    };
                //    lsPerson.Add(person);
                //}
                #endregion For Testing
                if (!id.HasValue || id.Value == 0) // At the initial time
                {
                    if (lsPerson == null)
                    {
                        lsPerson = new List<DAOPerson>();
                    }
                    mvcHtmlString = HtmlMvcHelpers.RenderPedigree(lsPerson);
                    PedigreeModel pedigreeModel = new PedigreeModel()
                    {
                        pedigree = mvcHtmlString,
                        messageNotify = msgNotify
                    };
                    return View(pedigreeModel);
                }
                else // At the nth time. Handle for maximum depth in recursion
                {
                    PedigreeModel pedigreeModel = new PedigreeModel();
                    pedigreeModel.messageNotify = msgNotify;
                    if (lsPerson != null)
                    {
                        daoPerson = lsPerson.Find(x => x.person_id == id);
                        if (daoPerson != null)
                        {
                            mvcHtmlString = HtmlMvcHelpers.RenderPedigree(daoPerson, lsPerson, null, null, true);
                            pedigreeModel.pedigree = mvcHtmlString;
                        }
                        else
                        {
                            pedigreeModel.messageNotify += "\n Can not find person!";
                        }
                    }
                    return View(pedigreeModel);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public ActionResult Detail(int id)
        {
            #region Declare local variables
            DAOPerson daoPerson = null;
            PersonModel person = null;
            BLPersonManager perMgnBl = null;
            string strMsgNotify = null;
            #endregion Declare local variables
            try
            {
                if (id == 0)
                {
                    daoPerson = new DAOPerson()
                    {
                        person_id = 0,
                        person_code = "01",
                        first_name = "family name",
                        middle_name = "family middle name",
                        last_name = "family last name",
                        birthday = "01/01/0001",
                        death_day = "01/01/0001"
                    };
                }
                else
                {
                    perMgnBl = new BLPersonManager(Constants.STR_CONN_NAME);
                    strMsgNotify = perMgnBl.DetailPerson(id, out daoPerson);
                }
                person = new PersonModel();
                person.person_id = id;
                if (daoPerson != null)
                {
                    if (!String.IsNullOrEmpty(daoPerson.person_code))
                        person.person_code = daoPerson.person_code;
                    if (!String.IsNullOrEmpty(daoPerson.first_name))
                        person.first_name = daoPerson.first_name;
                    if (!String.IsNullOrEmpty(daoPerson.middle_name))
                        person.middle_name = daoPerson.middle_name;
                    if (!String.IsNullOrEmpty(daoPerson.last_name))
                        person.last_name = daoPerson.last_name;
                    if (!String.IsNullOrEmpty(daoPerson.birthday))
                        person.birthday = daoPerson.birthday;
                    if (!String.IsNullOrEmpty(daoPerson.death_day))
                        person.death_day = daoPerson.death_day;
                    person.sex = String.IsNullOrEmpty(daoPerson.sex) ? "Y" : daoPerson.sex;
                    person.person_status = String.IsNullOrEmpty(daoPerson.person_status) ? "Y" : daoPerson.person_status;
                    person.chkSex = person.sex.Equals("Y") ? true : false;
                    person.chkStatus = person.chkStatus.Equals("Y") ? true : false;
                    person.strMsgNotify = "Get Detail Person Successfully!";
                }
                else
                {
                    person.strMsgNotify = "Get Detail Person Unsuccessfully!";
                }
                return PartialView("PersonDetail", person);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        [HttpPost]
        public ActionResult Detail(PersonModel person)
        {
            #region Declare local variables
            DAOPerson daoPerson = null;
            BLPersonManager perMgnBl = null;
            string strMsgNotify = null;
            #endregion Declare local variables
            try
            {
                daoPerson = new DAOPerson();
                daoPerson.person_id = person.person_id;
                if (!String.IsNullOrEmpty(person.person_code))
                    daoPerson.person_code = person.person_code;
                if (!String.IsNullOrEmpty(person.first_name))
                    daoPerson.first_name = person.first_name;
                if (!String.IsNullOrEmpty(person.middle_name))
                    daoPerson.middle_name = person.middle_name;
                if (!String.IsNullOrEmpty(person.last_name))
                    daoPerson.last_name = person.last_name;
                if (!String.IsNullOrEmpty(person.birthday))
                    daoPerson.birthday = person.birthday;
                if (!String.IsNullOrEmpty(person.death_day))
                    daoPerson.death_day = person.death_day;
                daoPerson.number_children = person.number_children;
                daoPerson.sex = person.chkSex ? "Y" : "N";
                daoPerson.person_status = person.chkStatus ? "Y" : "N";
                
                perMgnBl = new BLPersonManager(Constants.STR_CONN_NAME);
                strMsgNotify = perMgnBl.ModifyPerson(daoPerson);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public ActionResult AddPerson(int id, string strPerCd, bool isSibling)
        {
            #region Declare local variables
            PersonModel person = null;
            DAOPerson daoPerson = null;
            BLPersonManager perMgnBl = null;
            // The first time render, default max wife code, max brother code will be 0. 
            // It means person code will be generated without customization
            int intMaxWifeCd = 0;
            int intMaxCd = 0;
            // Get list of wife code, brother code for custom
            //List<int> lsMaxWifeCd = null;
            //List<int> lsMaxCd = null;
            string strOutPerCd = null;
            string strMsgNotify = null;
            #endregion Declare local variables
            try
            {
                // Generate person code correlative to  sibling mode or descendent mode
                daoPerson = new DAOPerson() { person_code = strPerCd };
                perMgnBl = new BLPersonManager(Constants.STR_CONN_NAME);
                //strMsgNotify = perMgnBl.GetMaxCodeByPerCd(daoPerson, isSibling, out strOutPerCd, intMaxWifeCd, intMaxCd);
                person = new PersonModel();
                person.strMsgNotify = strMsgNotify;
                if (!String.IsNullOrEmpty(strOutPerCd))
                {
                    person.person_code = strOutPerCd;
                }
                // Get list of wife code, brother code for custom

                return PartialView("AddingPerson", person);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        [HttpPost]
        public ActionResult AddPerson(PersonModel person)
        {
            #region Declare local variables
            BLPersonManager perMgnBl = null;
            string strMsgNotify = null;
            DAOPerson daoPerson = null;
            #endregion Declare local variables
            try
            {
                perMgnBl = new BLPersonManager(Constants.STR_CONN_NAME);
                daoPerson = person;
                strMsgNotify = perMgnBl.CreatePerson(daoPerson);
                return View("Index");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
